<?php
/**
 * 站内信管理
 * Created by PhpStorm.
 * User: 王海东
 * Date: 2018/12/26
 * Time: 13:33
 */

namespace app\adm\controller;

use app\common\model\Msg;

class MsgController extends BaseController
{
    /**
     * 发送站内信
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function sendMsg()
    {
        $in = $this->I([
            [
                "user_id|用户id",
                null,
                "d",
                "require",
            ],
            [
                "content|内容",
                null,
                "s",
                "require",
            ],
        ]);

        $m      = new Msg();
        if(!$m->send($in['user_id'],$in['content'])) {
            rj(1,'发送失败');
        }

        rj();
    }

    /**
     * 消息列表
     * @author 王海东
     * @date
     * @return void
     */
    public function lists()
    {
        $in = $this->I([
            [
                "name|收信人",
                null,
                "s",
            ],
            [
                "is_read|是否已读",
                null,
                "d",
            ],
            [
                "agent_name|代理商名称",
                null,
                "s",
            ],
            [
                "page|页码",
                DEF_PAGE_NO,
                "d",
            ],
            [
                "pageSize|页幅",
                DEF_PAGE_SIZE,
                "d",
            ]
        ]);

        $where = [
            'name'      => $in['name'],
            'is_read'   => $in['is_read'],
            'agent_name'=> $in['agent_name']
        ];

        $m      = new Msg();
        $list   = $m->list($where,$in['page'],$in['pageSize']);

        //梳理内容
        foreach ($list['list'] as &$v) {
            $v["content"] = htmlspecialchars_decode($v["content"]);//把一些预定义的 HTML 实体转换为字符
            $v["content"] = str_replace("&nbsp;", "", $v["content"]);//将空格替换成空
            $v["content"] = strip_tags($v["content"]);//函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
            $v["content"] = mb_substr($v["content"], 0, 40, "utf-8");//返回字符串中的前100字符串长度的字符
            if (mb_strlen($v["content"]) == 40) {
                $v["content"] .= "...";
            }
        }

        rjd([
            "list"  => $list['list'],
            "count" => $list['count'],
        ]);
    }

    /**
     * 站内信详情
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function msgDetail()
    {
        $in = $this->I([
            [
                "id|站内信编号",
                null,
                "d",
                "require"
            ]
        ]);

        $m      = new Msg();
        $list   = $m->detail($in['id']);

        $list["content"] = htmlspecialchars_decode($list["content"]);//把一些预定义的 HTML 实体转换为字符
        $list["content"] = str_replace("&nbsp;", "", $list["content"]);//将空格替换成空
        $list["content"] = strip_tags($list["content"]);//函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容

        rjd($list);
    }
}